﻿using System;

namespace Framework.ObjectPool
{
    /// <summary>
    /// 对象基类
    /// </summary>
    public abstract class ObjectBase
    {
        private string targetName;
        private string packageName;
        private string resName;
        private IObject targetObj;
        private DateTime lastUseTime;
        private bool isInUse;

        /// <summary>
        /// 对象名称
        /// </summary>
        public string Name
        {
            get
            {
                return targetName;
            }
            set
            {
                targetName = value;
            }
        }
        /// <summary>
        /// 资源包名称
        /// </summary>
        public string PackageName
        {
            get
            {
                return packageName;
            }
            set
            {
                packageName = value;
            }
        }
        /// <summary>
        /// 资源名称
        /// </summary>
        public string ResName
        {
            get
            {
                return resName;
            }
            set
            {
                resName = value;
            }
        }
        /// <summary>
        /// 获取对象
        /// </summary>
        public IObject TargetObject
        {
            get
            {
                return targetObj;
            }
            set
            {
                targetObj = value;
            }
        }
        /// <summary>
        /// 获取对象上次使用时间
        /// </summary>
        public DateTime LastUseTime
        {
            get
            {
                return lastUseTime;
            }
            set
            {
                lastUseTime = value;
            }
        }
        /// <summary>
        /// 是否正在使用
        /// </summary>
        public bool IsInUse
        {
            get
            {
                return isInUse;
            }
            set
            {
                isInUse = value;
            }
        }
        /// <summary>
        /// 创建对象基类
        /// </summary>
        public ObjectBase()
            : this(string.Empty, null)
        {

        }
        /// <summary>
        /// 创建对象基类
        /// </summary>
        /// <param name="target">对象</param>
        public ObjectBase(IObject target)
            : this(string.Empty, target)
        {

        }
        /// <summary>
        /// 创建对象基类
        /// </summary>
        /// <param name="name">对象名称</param>
        /// <param name="target">对象</param>
        public ObjectBase(string name, IObject target)
        {
            targetName = name;
            targetObj = target;
            lastUseTime = DateTime.Now;
            isInUse = false;
        }
        /// <summary>
        /// 注册对象时执行方法
        /// </summary>
        public virtual void OnRegister()
        {

        }
        /// <summary>
        /// 对象使用时执行方法
        /// </summary>
        public virtual void OnSpawn()
        {
            if (TargetObject != null)
                TargetObject.Spawn();
        }
        /// <summary>
        /// 对象回收时执行方法
        /// </summary>
        public virtual void OnCollect()
        {
            if (TargetObject != null)
                TargetObject.Collect();
        }
        /// <summary>
        /// 对象释放时执行方法
        /// </summary>
        public virtual void OnRelease()
        {
            if (TargetObject != null)
                TargetObject.Release();
        }
    }
}